<!--
 * @Description:缓动360开机
 * @Author: Sun
 * @Date: 2019-07-01 20:05:50
 * @LastEditors: Sun
 * @LastEditTime: 2019-07-25 22:49:56
 -->
<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
	<title>缓动360开机</title>
	<style>
		* {
			margin: 0;
			padding: 0
		}

		.box {
			width: 322px;
			position: fixed;
			bottom: 0;
			right: 0;
		}

		span {
			position: absolute;
			top: 0;
			right: 0;
			width: 30px;
			height: 20px;
			cursor: pointer;
		}

		.hd img {
			height: 300px;
			width: 100%;
		}

		.bd img {
			height: 150px;
			width: 100%;
		}
	</style>
</head>

<body>
	<div class="box" id="box">
		<span>缓动360开机</span>
		<div class="hd" id="t">
			<img src="./img/1.jpg" alt="">
		</div>
		<div class="bd" id="b">
			<img src="./img/2.jpg" alt="">
		</div>
	</div>
	<script>
		function $(id) {
			return document.getElementById(id);
		}
		window.onload = function () {
			var t = $("t")
			var b= $("b")
			var box= $("box")
			var closeAd=document.getElementsByTagName("span")[0];
			closeAd.onclick=function(){
				animate(b,{height:0},function(){
						animate(box,{width:0})
				})
			}

			//给谁 json
			function animate(obj, json, fn) {
				clearInterval(obj.timer)
				obj.timer = setInterval(() => {
					var flag = true //用来判断是否停止定时器
					//计算步长 动画原理 盒子本身样式+步长
					// //我们怎么知道我们当前的样式
					// //当前样式 （target-当前样式）/10==步长
					for (const key in json) {
						var current = parseInt(getStyle(obj, "", key));
						var step = (json[key] - current) / 10; //步长
						step = step > 0 ? Math.ceil(step) : Math.floor(step) //取整步长
						obj.style[key] = current + step + "px"
						//只要其中不满足条件 就不停止定时器
						if (current != json[key]) {
							flag = false
						}
					}

					if (flag) { //用于判断定时器的条件
						clearInterval(obj.timer)
						if (fn) {
							fn()
						}
					}

				}, 60)
			}

			function getStyle(ele, tum = null, attr) {
				if (window.getComputedStyle) { // 元素 伪类 css名字
					return window.getComputedStyle(ele, tum)[attr];
				}
				return ele.currentStyle[attr]; //ie
			}


		}
	</script>

</body>

</html>